//
//  LLT_ChangePasswordController_LXH.m
//  CoderLLT
//
//  Created by lanou3g on 15/12/21.
//  Copyright © 2015年 刘晓萌. All rights reserved.
//

#import "LLT_ChangePasswordController_LXH.h"
#import "UIViewController+LLT_LoginAndRegist_LXM.h"
#import  <AVOSCloud/AVOSCloud.h>

@interface LLT_ChangePasswordController_LXH ()<UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet UITextField *oldPasswordTextField;
@property (weak, nonatomic) IBOutlet UITextField *theNewPasswordTextField;
@property (weak, nonatomic) IBOutlet UITextField *anotherPasswordTextField;

@end

@implementation LLT_ChangePasswordController_LXH

#pragma mark --- dealloc
-(void)dealloc
{
    self.oldPasswordTextField.delegate = nil;
    self.theNewPasswordTextField.delegate = nil;
    self.anotherPasswordTextField.delegate = nil;
}

#pragma mark --- 加载视图完毕

- (void)viewDidLoad {
    [super viewDidLoad];
    [self makeDelegate];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

#pragma mark --- 接收代理

-(void)makeDelegate{
    self.oldPasswordTextField.delegate = self;
    self.theNewPasswordTextField.delegate = self;
    self.anotherPasswordTextField.delegate = self;
}

#pragma marke --- 完成textField代理
-(void)textFieldDidBeginEditing:(UITextField *)textField{
    textField.text = nil;
}


#pragma mark --- 点击事件

/**
 * 返回
 */
- (IBAction)didClickReturnButton:(UIButton *)sender {
    DissMissBack;
}

/**
 * 确认修改
 */
- (IBAction)didClickChangeButton:(UIButton *)sender {
    [self changPassword];
}


#pragma mark --- 功能实现

/**
 * 修改密码
 */
-(void)changPassword{
    //1.验证非空
    if (self.oldPasswordTextField.text.length > 0 && self.theNewPasswordTextField.text.length > 0 && self.anotherPasswordTextField.text.length > 0) {
        if ([self.theNewPasswordTextField.text isEqualToString:self.anotherPasswordTextField.text]) {
            NSString *oldPassword = [self md5StrWithString:self.oldPasswordTextField.text];
            NSString *newPassword = [self md5StrWithString:self.theNewPasswordTextField.text];
           // NSString *username = [[NSUserDefaults standardUserDefaults] dictionaryForKey:@"onlineUser"][@"username"];
           // [AVUser logInWithUsername:username password:oldPassword]; //请确保用户当前的有效登录状态
            [[AVUser currentUser] updatePassword:oldPassword newPassword:newPassword block:^(id object, NSError *error) {
                if (error) {
                    NSString *errorInfo = error.userInfo[@"error"];
                    void(^doForError)() = nil;
                    if ([errorInfo isEqualToString:@"The username and password mismatch."]) {
                        errorInfo = @"旧密码不正确";
                        doForError = ^{
                            self.oldPasswordTextField.text = nil;
                            self.theNewPasswordTextField.text = nil;
                            self.anotherPasswordTextField.text = nil;
                        };
                    }
                    //NSLog(@"%@",errorInfo);
                    [self showAlert:@"提 示" andAlertString:errorInfo andHandler:^(UIAlertAction *action) {
                        doForError();
                    }];
                }else{
                    [self showAlert:@"提 示" andAlertString:@"密码修改成功" andHandler:^(UIAlertAction *action) {
                        DissMissBack;
                    }];
                }
            }];
        }else{
            [self showAlert:@"提 示" andAlertString:@"新密码俩次输入不一致"];
        }
    }else{
        [self showAlert:@"提 示" andAlertString:@"请按要求填写"];
    }
}





/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
